﻿using System;
using System.Collections.Generic;
using System.Text;

namespace AMS.Common
{
    /// <summary>
    /// HTML构造器
    /// </summary>
    public class HtmlBuilder
    {
        /// <summary>
        /// HTML构造器
        /// </summary>
        public HtmlBuilder()
        {
            content = new StringBuilder();
        }

        /// <summary>
        /// 
        /// </summary>
        private StringBuilder content;

        /// <summary>
        /// 
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        public HtmlBuilder Append(string value)
        {
            content.Append(value);
            return this;
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="value1"></param>
        /// <param name="value2"></param>
        /// <returns></returns>
        public HtmlBuilder Append(string value1, string value2)
        {
            content.Append(value1);
            content.Append(value2);
            return this;
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        public HtmlBuilder AppendLine(string value)
        {
            content.AppendLine(value);
            return this;
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="value1"></param>
        /// <param name="value2"></param>
        /// <returns></returns>
        public HtmlBuilder AppendLine(string value1, string value2)
        {
            content.AppendLine(value1);
            content.AppendLine(value2);
            return this;
        }
        public HtmlBuilder AppendLine(string value1, string value2, string value3)
        {
            content.AppendLine(value1);
            content.AppendLine(value2);
            content.AppendLine(value3);
            return this;
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="format"></param>
        /// <param name="args"></param>
        /// <returns></returns>
        public HtmlBuilder AppendLineFormat(string format, params object[] args)
        {
            if (args == null || args.Length == 0)
            {
                content.AppendLine(format);
            }
            else
            {
                content.AppendFormat(format, args);
                content.AppendLine();
            }

            return this;
        }
        /// <summary>
        /// 缩进
        /// </summary>
        /// <returns></returns>
        public HtmlBuilder Indent()
        {
            return Indent(1);
        }

        /// <summary>
        /// 缩进N个单元
        /// </summary>
        /// <param name="count"></param>
        /// <returns></returns>
        public HtmlBuilder Indent(int count)
        {
            content.Append("".PadRight(count << 2, ' '));
            return this;
        }

        /// <summary>
        /// 添加TAG
        /// </summary>
        /// <param name="tag"></param>
        /// <returns></returns>
        public HtmlBuilder AddTag(string tag)
        {
            content.Append("<");
            content.Append(tag);
            content.Append("/>");
            return this;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="tag"></param>
        /// <param name="value"></param>
        /// <param name="closeTag">是否添加结束标签</param>
        /// <returns></returns>
        public HtmlBuilder AddTag(string tag, string value, bool closeTag)
        {
            content.Append("<");
            content.Append(tag);
            content.Append(">");
            content.Append(value);
            if (closeTag)
            {
                content.Append("</");
                content.Append(tag);
                content.AppendLine(">");
            }

            return this;
        }

        /// <summary>
        /// 添加一个Tag，并添加相应的属性，但不添加 </...>结束标签
        /// </summary>
        /// <param name="tag"></param>
        /// <param name="attributeName"></param>
        /// <param name="attributeValue"></param>
        /// <returns></returns>
        public HtmlBuilder AddTag(string tag, string attributeName, string attributeValue)
        {
            content.AppendFormat("<{0} {1}=\"{2}\">", tag, attributeName, attributeValue);
            content.AppendLine();
            return this;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="tag"></param>
        /// <param name="attributeName"></param>
        /// <param name="attributeValue"></param>
        /// <param name="innerHtml"></param>
        /// <returns></returns>
        public HtmlBuilder AddTag(string tag, string attributeName, string attributeValue, string innerHtml)
        {
            content.AppendFormat("<{0} {1}=\"{2}\">{3}</{4}>", tag, attributeName, attributeValue, innerHtml, tag);
            content.AppendLine();
            return this;
        }

        public override string ToString()
        {
            return content.ToString();
        }
    }
}
